package org.insia.smartposse.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.insia.smartposse.manager.UserManager;

public class UserRestrict implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {

		String login = (String) ((HttpServletRequest) req).getSession().getAttribute("user");
		// Check if a login User is save and set userManager with a UserManager
		UserManager u = new UserManager(login);
		((HttpServletRequest) req).getSession().setAttribute("usermanager", u);

		if (null != login && null != u){
			chain.doFilter(req, resp);
		} else {
			((HttpServletResponse) resp).sendRedirect(((HttpServletRequest) req).getContextPath() + "/login.do");
		}

	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
